#!/bin/bash

# 获取脚本所在的目录
DIR="$(cd "$(dirname "$0")" && pwd)"

# 获取同目录下的所有 jar 文件名
JAR_NAME=$(ls "$DIR"/*.jar 2>/dev/null | head -n 1)
if [ -z "$JAR_NAME" ]; then
    echo "No jar file found in current directory."
    exit 1
fi
FILE_NAME=$(basename "$JAR_NAME")
# 获取 jar 进程的 PID
PID=$(ps aux | grep "$FILE_NAME" | grep -v grep | awk '{print $2}')
if [ -z "$PID" ]; then
    echo "$FILE_NAME is not running."
    exit 1
fi

# 停止 jar 进程
echo "Stopping $FILE_NAME (PID $PID)..."
kill "$PID"

# 等待进程结束
while ps -p "$PID" > /dev/null; do
    sleep 1
done

echo "$FILE_NAME has been stopped."